<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="120px">
                <el-form-item label="商品SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="供应商" prop="supply_id">
                    <SelectPage
                        v-model="queryParams.supply_id"
                        placeholder="请选择供应商"
                        :url="$api.supply.getSupplyList"
                        show_field="supply_name"
                        query_field="supply_name"
                        query_field2="spu"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                        <el-upload
                            class="upload-demo"
                            ref="upload"
                            :action="api.upload"
                            :headers="headers"
                            :before-upload="handleBeforeUpload"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :on-success="handleUploadSuccess"
                            :on-error="handleUploadError"
                            :show-file-list="false">
                            <el-button slot="trigger" type="success">上传文件</el-button>
                        </el-upload>
                    </el-col> -->
                <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="ID" prop="id" min-width="40"></el-table-column>
                <el-table-column label="商品SPU" prop="spu" min-width=""></el-table-column>
                <el-table-column label="图片" min-width="100">
                    <template slot-scope="scope">
                        <ImageUploadImg v-model="scope.row.image_url" :readOnly="true"></ImageUploadImg>
                    </template>
                </el-table-column>
                <el-table-column label="商品名称" prop="product_name" min-width=""></el-table-column>
                <el-table-column label="当日销量" prop="current_nums" min-width="">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showDetailDialog(scope.row)">{{ scope.row.current_nums }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="当日缺货数量" prop="nums" min-width="">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showSkuListDialog(scope.row)">{{ scope.row.nums }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="供应商" prop="supply_name" min-width="180"></el-table-column>
                <el-table-column label="创建日期" prop="create_time" min-width=""></el-table-column>
                <!-- <el-table-column label="操作" min-width="100"  fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">编辑</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></detail-dialog>
        <sku-list-dialog v-model="skuListDialog.visible" v-if="skuListDialog.visible" :params="skuListDialog.data"></sku-list-dialog>
    </div>
</template>

<script>
import { exportFile } from '@/utils/index'
import { querygetStock } from '@/api/order'
import DetailDialog from './components/DetailDialog.vue'
import SkuListDialog from './components/SkuListDialog.vue'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'outstock',
    components: { DetailDialog, SkuListDialog },
   
    data() {
        return {
            api: {
                upload: process.env.VUE_APP_BASE_API + `/order/OrderCheckSpu/uploads`,
            },
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                ordercheck_id: undefined,
                spu: undefined,
                date: []
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            skuListDialog: {
                visible: false,
                data: {},
            },
        }
    },
    created() {
        this.getList()
    },
    methods: {
        showSkuListDialog(row) {
            this.skuListDialog.visible = true
            this.skuListDialog.data = row
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m'), parseTime(qyparams.create_time[1], 'y-m')]
            }
            querygetStock(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },

        

        handleBeforeUpload() {
            this.loading = true
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        
        // 上传成功回调
        handleUploadSuccess(res) {
            this.loading = false
            if (res.code == 200) {
                this.$message.success(res.msg)
            } else {
                this.$message.error(res.msg)
            }
            this.getList();
        },
        // 上传失败
        handleUploadError() {
            this.$message.error('上传失败')
            this.getList();
        },

    },
}
</script>
